﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.Drawing;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS对账明细生成信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMS_BillCreateController : ControllerBase
    {
        private readonly ILogger<TMS_BillCreateRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMS_BillCreateController(ILogger<TMS_BillCreateRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// TMS对账明细生成信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTMS_BillCreate([FromBody] TMS_BillCreateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMS_BillCreateResponseDto re = new TMS_BillCreateResponseDto();
            string waybill_no = strparm.waybill_no;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);
            decimal yprice = 0;
            string address_begin = "";
            string address_end = "";
            string price_no = "";
            string drive_distance = "";
            decimal materialamount = 0;
            string statementno = "DZ" + DateTime.Now.ToString("yyMMddHHmmss");
            decimal price = 0;
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic


            try
            {
                dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("waybill_no", waybill_no);//查询对应物流计划
                //DataTable fy_dt = MethodHelp.GetTableInfo("TMS0088", dic, con);//获取物流计划信息
                DataTable fy_dt = MethodHelp.GetTableInfo("TMS0170", dic, con);//获取物流计划信息
                if (fy_dt.Rows.Count>0)
                {
                    //foreach (DataRow item in fy_dt.Rows)
                    //{
                    //    materialamount = materialamount + DB.ToDecimal(item["ship_qty"].ToString());
                    //    string price_no_d = "";


                    //    dic = new Dictionary<string, string>(); //查询公用dic
                    //    dic.Add("start_address", item["shipping_city"].ToString());
                    //    dic.Add("end_address", item["delivery_city"].ToString());
                    //    dic.Add("cartype_code", item["cartype_code"].ToString());
                    //    dic.Add("carrierid_code", item["carrier_code"].ToString());
                    //    DataTable dt_price = MethodHelp.GetTableInfo("TMS0112", dic,con);//固定价，正向获取
                    //    if (dt_price.Rows.Count > 0)
                    //    {
                    //        price = DB.ToDecimal(dt_price.Rows[0]["tax_price"].ToString());
                    //        price_no_d = dt_price.Rows[0]["price_no"].ToString();
                    //    }
                    //    else
                    //    {
                    //        dic = new Dictionary<string, string>(); //查询公用dic
                    //        dic.Add("start_address", item["shipping_city"].ToString());
                    //        dic.Add("end_address", item["delivery_city"].ToString());
                    //        dic.Add("cartypecode", item["cartype_code"].ToString());
                    //        dic.Add("carrierid_code", item["carrier_code"].ToString());
                    //        DataTable dt_price_wf = MethodHelp.GetTableInfo("TMS0113", dic, con);//固定价，反向获取
                    //        if (dt_price_wf.Rows.Count>0)
                    //        {
                    //            price = DB.ToDecimal(dt_price_wf.Rows[0]["tax_price"].ToString());
                    //            price_no_d = dt_price_wf.Rows[0]["price_no"].ToString();
                    //        }                           
                    //    }                       
                    //    if (yprice == 0)
                    //    {
                    //        yprice = price;
                    //        address_begin = item["shipping_city"].ToString();
                    //        address_end = item["delivery_city"].ToString();
                    //        price_no = price_no_d;
                    //        drive_distance= item["drive_distance"].ToString();
                    //    }
                    //    else
                    //    {
                    //        if (yprice >= price)
                    //        {
                    //            yprice = price;
                    //            address_begin = item["shipping_city"].ToString();
                    //            address_end = item["delivery_city"].ToString();
                    //            price_no = price_no_d;
                    //            drive_distance = item["drive_distance"].ToString();
                    //        }
                    //    }

                    //}

                    dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("waybill_no", waybill_no);//查询运单出库总数量
                    DataTable ship_dt = MethodHelp.GetTableInfo("TMS0223", dic,con);
                    if (ship_dt.Rows.Count>0)
                    {
                        materialamount = DB.ToDecimal(ship_dt.Rows[0]["qty"].ToString());//当前运单数量
                    }



                    address_begin = fy_dt.Rows[0]["start_address"].ToString();
                    address_end = fy_dt.Rows[0]["end_address"].ToString();
                    yprice = DB.ToDecimal(fy_dt.Rows[0]["price_amount"].ToString());
                    price_no = fy_dt.Rows[0]["srm_priceno"].ToString();
                    if (strMsg == "")
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("factory_code", fy_dt.Rows[0]["factory_name"].ToString());
                        dic.Add("statementno", statementno);
                        dic.Add("shippingorder_no", waybill_no);
                        dic.Add("carriercode", fy_dt.Rows[0]["carrier_code"].ToString());
                        dic.Add("carriername", fy_dt.Rows[0]["carriername"].ToString());
                        dic.Add("carno", fy_dt.Rows[0]["car_no"].ToString());
                        dic.Add("cartypecode", fy_dt.Rows[0]["cartype_code"].ToString());
                        dic.Add("startaddress", address_begin);
                        dic.Add("endaddress", address_end);
                        dic.Add("material_amount", materialamount.ToString());
                        dic.Add("cprice", yprice.ToString());
                        dic.Add("finalprice", yprice.ToString());
                        dic.Add("cartypename", fy_dt.Rows[0]["cartypename"].ToString());
                        dic.Add("price_no", price_no);
                        dic.Add("drive_distance", "0");
                        dic.Add("ddate", fy_dt.Rows[0]["shipping_date"].ToString());
                        //strMsg = MethodHelp.InsertIntoTable("TMS0123", dic, con);
                        strMsg = MethodHelp.InsertIntoTable("TMS0173", dic, con);//发货日期取运单日期
                    }
                }                
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
                    
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
